package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper contentMapper;

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail) {
        // 如果没有设置序号，就自增
        if (strategyDetail.getSequence() == null) {
            int sequence = strategyDetailMapper.selectMaxSequence(strategyDetail.getCatalog().getId());
            strategyDetail.setSequence(sequence + 1);
        }
        // 如果是发布状态就设置发布时间
        if (strategyDetail.getState() == StrategyDetail.STATE_RELEASE) {
            strategyDetail.setReleaseTime(new Date());
        }

        // 内容
        StrategyContent content = strategyDetail.getStrategyContent();
        // 根据有没有detail的id判断是增加还是修改
        if(strategyDetail.getId()!=null){
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            content.setId(strategyDetail.getId());
            contentMapper.updateByPrimaryKey(content);
        }else{
            strategyDetailMapper.insert(strategyDetail);
            content.setId(strategyDetail.getId());
            contentMapper.insert(content);
        }
    }

    @Override
    public StrategyContent getContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public StrategyDetail getById(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo(list);
    }

}
